package com.mf.ergate.service.sys.impl;

import com.mf.ergate.api.sys.model.dto.FileStorageInfoSaveDto;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.dao.sys.FileStorageInfoDao;
import com.mf.ergate.domain.model.sys.FileStorageInfoEntity;
import com.mf.ergate.service.GenericService;
import com.mf.ergate.service.sys.FileStorageInfoService;
import com.mf.storage.service.ObjectStorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.util.UUID;

/**
 * 文件存储信息Service层实现类
 * User: zhaoming
 * DateTime: 2019-03-11
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class FileStorageInfoServiceImpl extends GenericService<FileStorageInfoEntity> implements FileStorageInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FileStorageInfoDao fileStorageInfoDao;
    @Autowired
    private ObjectStorageService objectStorageService;
    @Value("${storage.bucketName}")
    private String bucketName;

    /**
     * 保存
     * @param fileStorageInfoSaveDto 文件存储信息保存Dto
     * @return 文件存储信息
     */
    @Transactional
    public FileStorageInfoEntity save(FileStorageInfoSaveDto fileStorageInfoSaveDto){
        FileStorageInfoEntity fileStorageInfoEntity = MfBeanUtils.copyObject(fileStorageInfoSaveDto, FileStorageInfoEntity.class);
        String objectKey = UUID.randomUUID().toString() + "." + fileStorageInfoSaveDto.getFileSuffix();
        objectStorageService.putObject(bucketName, objectKey, new ByteArrayInputStream(fileStorageInfoSaveDto.getFile()));
        fileStorageInfoEntity.setFileStorageBucket(bucketName);
        fileStorageInfoEntity.setFileStorageKey(objectKey);
        fileStorageInfoEntity.setFileStorageUrl(objectStorageService.getObjectUrl(bucketName, objectKey));
        fileStorageInfoDao.insert(fileStorageInfoEntity);
        return fileStorageInfoEntity;
    }

    /**
     * 删除(逻辑删除)
     * @param fileStorageInfoEntity 文件存储信息Entity
     * @return 执行结果
     */
    @Transactional
    public Boolean delete(FileStorageInfoEntity fileStorageInfoEntity){
        String objectKey = fileStorageInfoDao.selectByUniqueNo(fileStorageInfoEntity.getUniqueNo()).getFileStorageKey();
        objectStorageService.deleteObject(bucketName, objectKey);
        return fileStorageInfoDao.discardByUniqueNo(fileStorageInfoEntity.getUniqueNo());
    }

    /**
     * 下载
     * @param uniqueNo 唯一编号
     * @return 文件
     */
    public byte[] download(String uniqueNo){
        FileStorageInfoEntity fileStorageInfoEntity = fileStorageInfoDao.selectByUniqueNo(uniqueNo);
        return objectStorageService.getObject(bucketName, fileStorageInfoEntity.getFileStorageKey());
    }
}
